//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using Microsoft.WindowsMobile.Samples.Location;
using System.Text;
using GpsSample;

namespace GpsTest
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class frmMain : System.Windows.Forms.Form
    {
        private System.Windows.Forms.MenuItem menuItemLog;
        private System.Windows.Forms.MainMenu mainMenu;
        private MenuItem menuItemGps;
        private MenuItem startGpsMenuItem;
        private MenuItem stopGpsMenuItem;

        private EventHandler updateDataHandler;
        GpsDeviceState device = null;
        GpsPosition position = null;
        private MenuItem menuItemMenu;
        private Label labelAmountSpent;
        private NumericTextBox textBoxAmountSpent;
        private MenuItem menuItemExit;
        private MenuItem menuItemClear;
        private MenuItem menuItemAbout;
        private Label labelNote;
        private TextBox textBoxNote;
        private WebBrowser webBrowser;

        Gps gps = new Gps();

        public frmMain()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
            this.mainMenu = new System.Windows.Forms.MainMenu();
            this.menuItemLog = new System.Windows.Forms.MenuItem();
            this.menuItemMenu = new System.Windows.Forms.MenuItem();
            this.menuItemGps = new System.Windows.Forms.MenuItem();
            this.startGpsMenuItem = new System.Windows.Forms.MenuItem();
            this.stopGpsMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItemClear = new System.Windows.Forms.MenuItem();
            this.menuItemAbout = new System.Windows.Forms.MenuItem();
            this.menuItemExit = new System.Windows.Forms.MenuItem();
            this.labelAmountSpent = new System.Windows.Forms.Label();
            this.textBoxAmountSpent = new GpsSample.NumericTextBox();
            this.labelNote = new System.Windows.Forms.Label();
            this.textBoxNote = new System.Windows.Forms.TextBox();
            this.webBrowser = new System.Windows.Forms.WebBrowser();
            this.SuspendLayout();
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.Add(this.menuItemLog);
            this.mainMenu.MenuItems.Add(this.menuItemMenu);
            // 
            // menuItemLog
            // 
            this.menuItemLog.Text = "&Log";
            this.menuItemLog.Click += new System.EventHandler(this.exitMenuItem_Click);
            // 
            // menuItemMenu
            // 
            this.menuItemMenu.MenuItems.Add(this.menuItemGps);
            this.menuItemMenu.MenuItems.Add(this.menuItemClear);
            this.menuItemMenu.MenuItems.Add(this.menuItemAbout);
            this.menuItemMenu.MenuItems.Add(this.menuItemExit);
            this.menuItemMenu.Text = "&Menu";
            // 
            // menuItemGps
            // 
            this.menuItemGps.MenuItems.Add(this.startGpsMenuItem);
            this.menuItemGps.MenuItems.Add(this.stopGpsMenuItem);
            this.menuItemGps.Text = "GPS";
            // 
            // startGpsMenuItem
            // 
            this.startGpsMenuItem.Text = "Start GPS";
            this.startGpsMenuItem.Click += new System.EventHandler(this.startGpsMenuItem_Click);
            // 
            // stopGpsMenuItem
            // 
            this.stopGpsMenuItem.Enabled = false;
            this.stopGpsMenuItem.Text = "Stop GPS";
            this.stopGpsMenuItem.Click += new System.EventHandler(this.stopGpsMenuItem_Click);
            // 
            // menuItemClear
            // 
            this.menuItemClear.Text = "&Clear";
            this.menuItemClear.Click += new System.EventHandler(this.menuItemClear_Click);
            // 
            // menuItemAbout
            // 
            this.menuItemAbout.Text = "&About";
            this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
            // 
            // menuItemExit
            // 
            this.menuItemExit.Text = "E&xit";
            this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
            // 
            // labelAmountSpent
            // 
            this.labelAmountSpent.Location = new System.Drawing.Point(3, 5);
            this.labelAmountSpent.Name = "labelAmountSpent";
            this.labelAmountSpent.Size = new System.Drawing.Size(115, 20);
            this.labelAmountSpent.Text = "Amount Spent:";
            // 
            // textBoxAmountSpent
            // 
            this.textBoxAmountSpent.Location = new System.Drawing.Point(122, 4);
            this.textBoxAmountSpent.Name = "textBoxAmountSpent";
            this.textBoxAmountSpent.Size = new System.Drawing.Size(115, 21);
            this.textBoxAmountSpent.TabIndex = 2;
            this.textBoxAmountSpent.Text = "0.00";
            // 
            // labelNote
            // 
            this.labelNote.Location = new System.Drawing.Point(3, 32);
            this.labelNote.Name = "labelNote";
            this.labelNote.Size = new System.Drawing.Size(113, 20);
            this.labelNote.Text = "Note:";
            // 
            // textBoxNote
            // 
            this.textBoxNote.Location = new System.Drawing.Point(122, 31);
            this.textBoxNote.Name = "textBoxNote";
            this.textBoxNote.Size = new System.Drawing.Size(115, 21);
            this.textBoxNote.TabIndex = 9;
            // 
            // webBrowser
            // 
            this.webBrowser.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.webBrowser.Location = new System.Drawing.Point(0, 58);
            this.webBrowser.Name = "webBrowser";
            this.webBrowser.Size = new System.Drawing.Size(240, 210);
            // 
            // frmMain
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.webBrowser);
            this.Controls.Add(this.textBoxNote);
            this.Controls.Add(this.labelNote);
            this.Controls.Add(this.textBoxAmountSpent);
            this.Controls.Add(this.labelAmountSpent);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Menu = this.mainMenu;
            this.MinimizeBox = false;
            this.Name = "frmMain";
            this.Text = "GeoCash";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Closed += new System.EventHandler(this.Form1_Closed);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        static void Main() 
        {
            Application.Run(new frmMain());
        }

        string documentText = string.Empty;
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            double amount = Double.Parse(textBoxAmountSpent.Text);
            
            GpsPosition position = gps.GetPosition();
            
            if(position != null)
            {
                if(position.LatitudeValid && position.LongitudeValid)
                {
                    sb.AppendFormat(@"<b>Date:</b> {0}", DateTime.Now).Append("<br/>");
                    sb.AppendFormat(@"<b>Amount:</b> <span style=""color:red"">{0:c}</span>", decimal.Parse(textBoxAmountSpent.Text)).Append("<br/>");
                    sb.AppendFormat(@"<b>Note:</b> {0}", textBoxNote.Text).Append("<br/>");
                    sb.AppendFormat(@"<b>Latitude:</b> {0}", position.Latitude).Append("<br/>");
                    sb.AppendFormat(@"<b>Longitude:</b> {0}", position.Longitude).Append("<br/>");
                    sb.Append("<br/>");

                    documentText += sb.ToString();
                    webBrowser.DocumentText = documentText;
                }
            }
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            //updateDataHandler = new EventHandler(UpdateData);

            //gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
            //gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
        }

        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            position = args.Position;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            Invoke(updateDataHandler);

        }

        void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
        {
            device = args.DeviceState;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            Invoke(updateDataHandler);
        }

        //void UpdateData(object sender, System.EventArgs args)
        //{
        //    if (gps.Opened)
        //    {
        //        string str = "";
        //        if (device != null)
        //        {
        //            str = device.FriendlyName + " " + device.ServiceState + ", " + device.DeviceState + "\n";
        //        }

        //        if (position != null)
        //        {

        //            if (position.LatitudeValid)
        //            {
        //                str += "Latitude (DD):\n   " + position.Latitude + "\n";
        //                str += "Latitude (D,M,S):\n   " + position.LatitudeInDegreesMinutesSeconds + "\n";
        //            }

        //            if (position.LongitudeValid)
        //            {
        //                str += "Longitude (DD):\n   " + position.Longitude + "\n";
        //                str += "Longitude (D,M,S):\n   " + position.LongitudeInDegreesMinutesSeconds + "\n";
        //            }

        //            if (position.SatellitesInSolutionValid &&
        //                position.SatellitesInViewValid &&
        //                position.SatelliteCountValid)
        //            {
        //                str += "Satellite Count:\n   " + position.GetSatellitesInSolution().Length + "/" +
        //                    position.GetSatellitesInView().Length + " (" +
        //                    position.SatelliteCount + ")\n";
        //            }

        //            if (position.TimeValid)
        //            {
        //                str += "Time:\n   " + position.Time.ToString() + "\n";
        //            }
        //        }

        //        status.Text = str;

        //    }
        //}

        private void Form1_Closed(object sender, System.EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }
        }

        private void stopGpsMenuItem_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }

            startGpsMenuItem.Enabled = true;
            stopGpsMenuItem.Enabled = false;
        }

        private void startGpsMenuItem_Click(object sender, EventArgs e)
        {
            if (!gps.Opened)
            {
                gps.Open();
            }

            startGpsMenuItem.Enabled = false;
            stopGpsMenuItem.Enabled = true;
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }

            Close();
        }

        private void menuItemClear_Click(object sender, EventArgs e)
        {
            documentText = string.Empty;
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }
    }
}
